import router from './router'
import storage from 'store'
import NProgress from 'nprogress' // progress bar
import '@/components/NProgress/nprogress.less' // progress bar custom style
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { axiosPromiseArr } from "@/utils/request";
import { Modal } from 'ant-design-vue'
import { loginAuth } from '@/api/api'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const allowList = ['login', 'register', 'registerResult'] // no redirect allowList
const loginRoutePath = '/user/login'
// storage.set(ACCESS_TOKEN, 'eyJraWQiOiJ2WjNRcERuSHNWY25YRlN0ck8ySlR2ckxwbHhudG4wMnluYU93bUhocmNrIiwiYWxnIjoiSFM1MTIifQ.eyJpc3MiOiJJZGVhaW8iLCJpYXQiOjE2NTIzNDA5NzYsImp0aSI6IjczOTM5MzFhLWI3OTctNGE4ZS05MzFlLTMzNTc3NzUzYzMwNiIsInVzZXIiOnsiaWQiOjQsImVtYWlsIjoiMTIwOGRvZ2xhc0BnbWFpbC5jb20ifX0.dIyytvgH_SN0fAq9S72yi1x6U7pYtdubpuweXqM3c9WAxzsNFMdYaU_xREBeEa2QkbJh00Q2jIvDDNX_qAMioA')
router.beforeEach(async (to, from, next) => {
  console.log(to.query);
  /**
   *  模拟登录
   */
  if (to.name === 'login' && to.query.code && to.query.state) {
    const loginRes = await loginAuth({ code: to.query.code, state: to.query.state })
    storage.set(ACCESS_TOKEN, loginRes.token)
    next({
      path: '/'
    })
    NProgress.done()
    return
  }
  NProgress.start() // start progress bar
  /* has token */
  if (storage.get(ACCESS_TOKEN)) {
    next()
  } else {
    if (allowList.includes(to.name)) {
      // 在免登录名单，直接进入
      next()
    } else {
      next({ path: loginRoutePath, query: { redirect: to.fullPath } })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
  // if ((from.name === 'DashboardEdit' && to.name !== 'DashboardEditForm') || (from.name === 'DashboardCreate' && to.name !== 'DashboardCreateForm')
  //     || (from.name === 'DashboardEditForm' && to.name !== 'DashboardEdit') || (from.name === 'DashboardCreateForm' && to.name !== 'DashboardCreate')
  //   ) {
  //   Modal.confirm({
  //     title: () => 'Are you sure?',
  //     content: () => 'Your progress won’t be saved and you haven’t completed the creation.',
  //     okText: 'Yes, leave',
  //     cancelText: 'No, continue',
  //     onOk () {
  //       NProgress.start() // start progress bar
  //       /* has token */
  //       if (storage.get(ACCESS_TOKEN)) {
  //         next()
  //       } else {
  //         if (allowList.includes(to.name)) {
  //           // 在免登录名单，直接进入
  //           next()
  //         } else {
  //           next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  //           NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  //         }
  //       }
  //     },
  //     onCancel () {
  //       console.log('Cancel')
  //     }
  //   })
  // } else {
  //   NProgress.start() // start progress bar
  //   /* has token */
  //   if (storage.get(ACCESS_TOKEN)) {
  //     next()
  //   } else {
  //     if (allowList.includes(to.name)) {
  //       // 在免登录名单，直接进入
  //       next()
  //     } else {
  //       next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  //       NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  //     }
  //   }
  // }

})

router.afterEach(() => {
  // 路由切换时取消所有未完成的axios请求
  axiosPromiseArr.forEach((ele, index) => {
    ele.f()
    delete axiosPromiseArr[index]
  })
  NProgress.done() // finish progress bar
})
